package cc.linker.steplink.crm.controller;

import cc.linker.steplink.crm.commons.Constants;
import cc.linker.steplink.crm.commons.domain.WxUser;
import cc.linker.steplink.crm.merchanth5.commons.RequestFilter;
import cc.linker.steplink.crm.merchanth5.service.LiveGoodsService;
import cc.linker.steplink.crm.merchanth5.service.common.SmsService;
import cc.linker.steplink.crm.response.GlobalException;
import cc.linker.steplink.crm.service.CustomerService;
import cc.linker.steplink.crm.service.ICreationAwardService;
import com.mongodb.util.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: CKQ
 * @DateTime: 2020/5/26 18:40
 * @Description: TODO
 */
@RestController
@Slf4j
@RequestMapping("/h5/creationAward")
public class CreationAwardController {

    @Autowired
    private CustomerService customerService;

    @Autowired
    private SmsService smsService;

    @Autowired
    private LiveGoodsService liveGoodsService;

    @Autowired
    private ICreationAwardService creationAwardService;

    @Autowired
    private RequestFilter requestFilter;

    //发送短信
    @GetMapping("sendVerificationCode")
    public Object sendVerificationCode( @RequestParam(value = "phone") String phone, HttpServletRequest request) throws Exception {
        if (StringUtils.isEmpty(phone)) {
            throw GlobalException.build("联系电话不能为空");
        }
        WxUser wxUser = requestFilter.getWxUser(request);
        Map<String, Object> liveList = liveGoodsService.getLiveInfoByAccount(phone, wxUser.getOrgId());
        if (StringUtils.isEmpty(liveList)) {
            throw GlobalException.build("此手机号未创建直播号账号");
        }
        if (customerService.isExitLiveAccountCustomer(phone,wxUser.getOrgId())) {
            throw GlobalException.build("此手机号已经绑定其他账号");
        }
        Map<String, Object> nvps = new HashMap<>();
        nvps.put("businessType", "1020");
        nvps.put("mobilNumber", phone);
        nvps.put("params", "");
        nvps.put("token", "");
        String result = smsService.sendVerification(wxUser.getToken(), nvps);
        Object parse = JSON.parse(result);
        Map ret = (Map) parse;
        if ((Integer) ret.get("rt") != 1) {
            throw GlobalException.build("验证码发送失败");
        }
        return true;
    }

    //绑定直播间账号
    @PostMapping("/bindLiveAccount")
    public Boolean bindLiveAccount(@RequestBody Map<String, Object> params,
                                   HttpServletRequest request) throws Exception {
        WxUser wxUser = requestFilter.getWxUser(request);

        //如果PHONE和code 都为空则解除绑定
       if((!params.containsKey("phone") || StringUtils.isEmpty(params.get("phone").toString())) &&
               (!params.containsKey("code") || StringUtils.isEmpty(params.get("code").toString()))) {

           if (!customerService.updateLiveAccountByCustomerId(wxUser.getCustomerId(), "")) {
               throw GlobalException.build("解除绑定直播号失败");
           }
           return true;
       }

        // 系统 验证码判断
        Map<String, Object> nvps = new HashMap<>();
        nvps.put("businessType", "1020");
        nvps.put("mobilNumber", params.get("phone").toString());
        nvps.put("randomCode", params.get("code").toString());
        String result = smsService.checkVerification(wxUser.getToken(), nvps);
        Map ret = (Map) JSON.parse(result);
        if ((Integer) ret.get("rt") != 1) {
            throw GlobalException.build("验证码失效");
        }
        if (!customerService.updateLiveAccountByCustomerId(wxUser.getCustomerId(), params.get("phone").toString())) {
            throw GlobalException.build("绑定直播号失败");
        }
        return true;
    }

    //通过融媒号ID获取绑定用户的ID
    @GetMapping("getCustomerIdByMarketing")
    public Object getCustomerIdByMarketing(@RequestParam(value = "marketingId") String marketingId, HttpServletRequest request) throws Exception {
        String customerId = customerService.getCustomerIdByMarketing(marketingId);
        Map<String, Object> map = new HashMap<>();
        map.put("customerId", customerId);
        return map;
    }

    //绑定营销号
    @PostMapping("/bindMarketingAccount")
    public Object bindMarketingAccount(@RequestBody Map<String, Object> params,
                                       HttpServletRequest request) throws Exception {
        WxUser wxUser = requestFilter.getWxUser(request);
        if (!params.containsKey("userCode") || StringUtils.isEmpty(params.get("userCode").toString())) {
            throw GlobalException.build("userCode不能为空");
        }
        if (!params.containsKey("pwd") || StringUtils.isEmpty(params.get("pwd").toString())) {
            throw GlobalException.build("pwd不能为空");
        }
        return creationAwardService.bindMarketingAccount(wxUser.getCustomerId(), wxUser.getOrgId(), params.get("userCode").toString(), params.get("pwd").toString());
    }

    //解除绑定
    @PostMapping("/unBindMarketingAccount")
    public Object unBindMarketingAccount(HttpServletRequest request)  throws Exception {
        WxUser wxUser = requestFilter.getWxUser(request);
        return creationAwardService.bindMarketingAccount(wxUser.getCustomerId(),"","","");
    }
}